﻿from django.db import models
from .constants import DATA_TYPES
from django.utils import timezone

###############################################1.股票基本信息#######################################################
class StockBasic(models.Model):
    """股票基本信息"""
    ts_code = models.CharField(max_length=10, primary_key=True, verbose_name='TS代码')
    symbol = models.CharField(max_length=10, verbose_name='股票代码')
    name = models.CharField(max_length=100, verbose_name='股票名称')
    area = models.CharField(max_length=50, null=True, blank=True, verbose_name='所在地域')
    industry = models.CharField(max_length=50, null=True, blank=True, verbose_name='所属行业')
    market = models.CharField(max_length=50, null=True, blank=True, verbose_name='市场类型')
    list_date = models.CharField(max_length=8, null=True, blank=True, verbose_name='上市日期')
    is_hs = models.CharField(max_length=1, null=True, blank=True, verbose_name='是否沪深港通标的')
    act_name = models.CharField(max_length=50, null=True, blank=True, verbose_name='实控人名称')
    act_ent_type = models.CharField(max_length=10, null=True, blank=True, verbose_name='实控人企业性质')

    class Meta:
        verbose_name = '股票基本信息'
        verbose_name_plural = verbose_name
        db_table = 'stock_set_basic'
        ordering = ['ts_code']

    def __str__(self):
        return f"{self.ts_code} - {self.name}"

###################################################2.交易日历####################################################
class TradeDate(models.Model):
    """交易日历"""
    exchange = models.CharField(max_length=10, verbose_name='交易所代码')
    cal_date = models.CharField(max_length=8, verbose_name='日历日期')
    is_open = models.IntegerField(verbose_name='是否交易')
    pretrade_date = models.CharField(max_length=8, null=True, blank=True, verbose_name='上一个交易日')

    class Meta:
        verbose_name = '交易日历'
        verbose_name_plural = verbose_name
        db_table = 'stock_trade_cal'
        ordering = ['-cal_date']
        unique_together = ['exchange', 'cal_date']

    def __str__(self):
        return f"{self.exchange} - {self.cal_date} - {'交易' if self.is_open else '休市'}"
###################################################3.股票日K线数据####################################################
#还是将所有个股都放在一个表中，方便查询
class DailyData(models.Model):
    """日线行情"""
    ts_code = models.CharField(max_length=10, verbose_name='TS代码')
    trade_date = models.CharField(max_length=8, verbose_name='交易日期')
    open = models.FloatField(null=True, blank=True, verbose_name='开盘价')
    high = models.FloatField(null=True, blank=True, verbose_name='最高价')
    low = models.FloatField(null=True, blank=True, verbose_name='最低价')
    close = models.FloatField(null=True, blank=True, verbose_name='收盘价')
    pre_close = models.FloatField(null=True, blank=True, verbose_name='昨收价')
    change = models.FloatField(null=True, blank=True, verbose_name='涨跌额')
    pct_chg = models.FloatField(null=True, blank=True, verbose_name='涨跌幅')
    vol = models.FloatField(null=True, blank=True, verbose_name='成交量')
    amount = models.FloatField(null=True, blank=True, verbose_name='成交额')

    class Meta:
        verbose_name = '日线行情'
        verbose_name_plural = verbose_name
        db_table = 'stock_kdata_daily'
        ordering = ['-trade_date']
        unique_together = ['ts_code', 'trade_date']

    def __str__(self):
        return f"{self.ts_code} - {self.trade_date}"


##################################################4.指数日线数据####################################################
class IndexDaily(models.Model):
    """指数日线行情"""
    ts_code = models.CharField(max_length=20, verbose_name='TS代码')
    trade_date = models.CharField(max_length=8, verbose_name='交易日期')
    open = models.FloatField(null=True, blank=True, verbose_name='开盘点位')
    high = models.FloatField(null=True, blank=True, verbose_name='最高点位')
    low = models.FloatField(null=True, blank=True, verbose_name='最低点位')
    close = models.FloatField(null=True, blank=True, verbose_name='收盘点位')
    pre_close = models.FloatField(null=True, blank=True, verbose_name='昨收点位')
    change = models.FloatField(null=True, blank=True, verbose_name='涨跌点')
    pct_chg = models.FloatField(null=True, blank=True, verbose_name='涨跌幅')
    vol = models.FloatField(null=True, blank=True, verbose_name='成交量(手)')
    amount = models.FloatField(null=True, blank=True, verbose_name='成交额(千元)')
    total_mv = models.FloatField(null=True, blank=True, verbose_name='总市值(亿元)')
    float_mv = models.FloatField(null=True, blank=True, verbose_name='流通市值(亿元)')
    pe = models.FloatField(null=True, blank=True, verbose_name='市盈率')
    pb = models.FloatField(null=True, blank=True, verbose_name='市净率')
    turnover_rate_f = models.FloatField(null=True, blank=True, verbose_name='换手率(基于自由流通股本)')

    class Meta:
        verbose_name = '指数日线行情'
        verbose_name_plural = verbose_name
        db_table = 'stock_index_daily'
        ordering = ['-trade_date']
        unique_together = ['ts_code', 'trade_date']
        indexes = [
            models.Index(fields=['trade_date'], name='index_daily_date_idx'),
        ]

    def __str__(self):
        return f"{self.ts_code} - {self.trade_date}"



#################################################5.指数周线数据####################################################
class IndexWeekly(models.Model):
    """指数周线行情"""
    ts_code = models.CharField(max_length=20, verbose_name='TS代码')
    trade_date = models.CharField(max_length=8, verbose_name='交易日期')
    open = models.FloatField(null=True, blank=True, verbose_name='开盘点位')
    high = models.FloatField(null=True, blank=True, verbose_name='最高点位')
    low = models.FloatField(null=True, blank=True, verbose_name='最低点位')
    close = models.FloatField(null=True, blank=True, verbose_name='收盘点位')
    pre_close = models.FloatField(null=True, blank=True, verbose_name='昨收点位')
    change = models.FloatField(null=True, blank=True, verbose_name='涨跌点')
    pct_chg = models.FloatField(null=True, blank=True, verbose_name='涨跌幅')
    vol = models.FloatField(null=True, blank=True, verbose_name='成交量(手)')
    amount = models.FloatField(null=True, blank=True, verbose_name='成交额(千元)')

    class Meta:
        verbose_name = '指数周线行情'
        verbose_name_plural = verbose_name
        db_table = 'stock_index_weekly'
        ordering = ['-trade_date']
        unique_together = ['ts_code', 'trade_date']
        indexes = [
            models.Index(fields=['trade_date'], name='index_weekly_date_idx'),
        ]

    def __str__(self):
        return f"{self.ts_code} - {self.trade_date}"



#################################################6.指数月线数据####################################################
class IndexMonthly(models.Model):
    """指数月线行情"""
    ts_code = models.CharField(max_length=20, verbose_name='TS代码')
    trade_date = models.CharField(max_length=8, verbose_name='交易日期')
    open = models.FloatField(null=True, blank=True, verbose_name='开盘点位')
    high = models.FloatField(null=True, blank=True, verbose_name='最高点位')
    low = models.FloatField(null=True, blank=True, verbose_name='最低点位')
    close = models.FloatField(null=True, blank=True, verbose_name='收盘点位')
    pre_close = models.FloatField(null=True, blank=True, verbose_name='昨收点位')
    change = models.FloatField(null=True, blank=True, verbose_name='涨跌点')
    pct_chg = models.FloatField(null=True, blank=True, verbose_name='涨跌幅')
    vol = models.FloatField(null=True, blank=True, verbose_name='成交量(手)')
    amount = models.FloatField(null=True, blank=True, verbose_name='成交额(千元)')

    class Meta:
        verbose_name = '指数月线行情'
        verbose_name_plural = verbose_name
        db_table = 'stock_index_monthly'
        ordering = ['-trade_date']
        unique_together = ['ts_code', 'trade_date']
        indexes = [
            models.Index(fields=['trade_date'], name='index_monthly_date_idx'),
        ]

    def __str__(self):
        return f"{self.ts_code} - {self.trade_date}"


##################################################7.个股每日基本指标数据####################################################
class DailyBasic(models.Model):
    """每日指标"""
    ts_code = models.CharField(max_length=10, verbose_name='TS代码')
    trade_date = models.CharField(max_length=8, verbose_name='交易日期')
    close = models.FloatField(null=True, blank=True, verbose_name='当日收盘价')
    turnover_rate = models.FloatField(null=True, blank=True, verbose_name='换手率')
    turnover_rate_f = models.FloatField(null=True, blank=True, verbose_name='换手率(自由流通股)')
    volume_ratio = models.FloatField(null=True, blank=True, verbose_name='量比')
    pe = models.FloatField(null=True, blank=True, verbose_name='市盈率(总市值/净利润)')
    pe_ttm = models.FloatField(null=True, blank=True, verbose_name='市盈率(TTM)')
    pb = models.FloatField(null=True, blank=True, verbose_name='市净率(总市值/净资产)')
    ps = models.FloatField(null=True, blank=True, verbose_name='市销率')
    ps_ttm = models.FloatField(null=True, blank=True, verbose_name='市销率(TTM)')
    dv_ratio = models.FloatField(null=True, blank=True, verbose_name='股息率')
    dv_ttm = models.FloatField(null=True, blank=True, verbose_name='股息率(TTM)')
    total_share = models.FloatField(null=True, blank=True, verbose_name='总股本(万股)')
    float_share = models.FloatField(null=True, blank=True, verbose_name='流通股本(万股)')
    free_share = models.FloatField(null=True, blank=True, verbose_name='自由流通股本(万股)')
    total_mv = models.FloatField(null=True, blank=True, verbose_name='总市值(万元)')
    circ_mv = models.FloatField(null=True, blank=True, verbose_name='流通市值(万元)')

    class Meta:
        verbose_name = '每日指标'
        verbose_name_plural = verbose_name
        db_table = 'stock_daily_basic'
        ordering = ['-trade_date']
        unique_together = ['ts_code', 'trade_date']

    def __str__(self):
        return f"{self.ts_code} - {self.trade_date}"



##################################################8.数据更新记录####################################################
class DataUpdateRecord(models.Model):
    """数据更新记录"""
    data_type = models.CharField(
        max_length=50,
        unique=True,
        choices=[(v, v) for v in DATA_TYPES.values()],
        verbose_name='数据类型'
    )
    #通过最后更新日期来判断下次更新的起始日期
    last_update_date = models.DateField(verbose_name='最后更新日期')

    class Meta:
        verbose_name = '数据更新记录'
        verbose_name_plural = verbose_name
        db_table = 'stock_data_update_record'
        ordering = ['data_type']

    def __str__(self):
        return f"{self.data_type} - {self.last_update_date}"

    @classmethod
    def get_last_update_date(cls, data_type):
        """获取指定数据类型的最后更新日期"""
        from .constants import START_KDATA_DATE
        record = cls.objects.filter(data_type=data_type).first()
        return record.last_update_date if record else START_KDATA_DATE

    @classmethod
    def update_record(cls, data_type, update_date):
        """更新指定数据类型的最后更新日期"""
        record, created = cls.objects.update_or_create(
            data_type=data_type,
            defaults={'last_update_date': update_date}
        )
        return record

##################################################9.同花顺板块数据####################################################
class ThsPlate(models.Model):
    """同花顺板块（包括行业和概念）"""
    ts_code = models.CharField(max_length=20, primary_key=True, verbose_name='板块代码')
    name = models.CharField(max_length=100, verbose_name='板块名称')
    count = models.IntegerField(verbose_name='成分股数量')
    exchange = models.CharField(max_length=10, verbose_name='交易所')
    list_date = models.CharField(max_length=8, null=True, blank=True, verbose_name='上市日期')
    type = models.CharField(max_length=1, verbose_name='板块类型', help_text='I-行业，N-概念')
    
    class Meta:
        verbose_name = '同花顺板块'
        verbose_name_plural = verbose_name
        db_table = 'stock_ths_plate'
        indexes = [
            models.Index(fields=['name'], name='ths_plate_name_idx'),
            models.Index(fields=['type'], name='ths_plate_type_idx'),
        ]
    
    def __str__(self):
        plate_type = "行业" if self.type == 'I' else "概念"
        return f"{self.name} ({plate_type})"
##################################################10.同花顺板块成分数据####################################################
class ThsStockPlate(models.Model):
    """同花顺板块成分股"""
    ts_code = models.CharField(max_length=20, verbose_name="板块代码")#不能做主键，因为不能唯一
    con_code = models.CharField(max_length=20, verbose_name="成分股代码") 
    con_name = models.CharField(max_length=50, verbose_name="成分股名称")
    is_new = models.BooleanField(default=False, verbose_name="是否新增个股")

    class Meta:
        db_table = 'stock_ths_stock_plate'
        unique_together = ('ts_code', 'con_code')
        verbose_name = '同花顺板块成分'
        verbose_name_plural = verbose_name

    def __str__(self):
        return f"{self.ts_code}:{self.con_name}"

#################################################11.同花顺板块指数行情####################################################
class ThsPlateQuote(models.Model):
    """同花顺板块指数行情"""
    ts_code = models.CharField(max_length=20, verbose_name='板块代码')
    trade_date = models.CharField(max_length=8, verbose_name='交易日期')
    open = models.FloatField(null=True, blank=True, verbose_name='开盘点位')
    high = models.FloatField(null=True, blank=True, verbose_name='最高点位')
    low = models.FloatField(null=True, blank=True, verbose_name='最低点位')
    close = models.FloatField(null=True, blank=True, verbose_name='收盘点位')
    pre_close = models.FloatField(null=True, blank=True, verbose_name='昨收点位')
    change = models.FloatField(null=True, blank=True, verbose_name='涨跌点位')
    pct_chg = models.FloatField(null=True, blank=True, verbose_name='涨跌幅')
    vol = models.FloatField(null=True, blank=True, verbose_name='成交量')
    turnover_rate = models.FloatField(null=True, blank=True, verbose_name='换手率')
    float_mv= models.FloatField(null=True, blank=True, verbose_name='流通市值')
    
    class Meta:
        verbose_name = '同花顺板块指数行情'
        verbose_name_plural = verbose_name
        db_table = 'stock_ths_plate_kdata'
        ordering = ['-trade_date']
        unique_together = ['ts_code', 'trade_date']
        indexes = [
            models.Index(fields=['trade_date'], name='ths_plate_quote_date_idx'),
        ]

    def __str__(self):
        return f"{self.ts_code} - {self.trade_date}"



#################################################12.个股分时数据####################################################
class StockMinuteData(models.Model):
    """个股分时数据(1分钟)"""
    ts_code = models.CharField(max_length=10, verbose_name='TS代码')
    trade_time = models.DateTimeField(verbose_name='交易时间')
    open = models.FloatField(verbose_name='开盘价')
    high = models.FloatField(verbose_name='最高价')
    low = models.FloatField(verbose_name='最低价')
    close = models.FloatField(verbose_name='收盘价')
    vol = models.FloatField(verbose_name='成交量(手)')
    amount = models.FloatField(verbose_name='成交额(千元)')
    avg_price = models.FloatField(verbose_name='均价',default=0)#个股有均价，指数没有
    
    class Meta:
        verbose_name = '个股分时数据'
        verbose_name_plural = verbose_name
        db_table = 'stock_minute_data'
        ordering = ['-trade_time']
        unique_together = ['ts_code', 'trade_time']
        indexes = [
            models.Index(fields=['trade_time'], name='stock_minute_time_idx'),
        ]

    def __str__(self):
        return f"{self.ts_code} - {self.trade_time}"
    
#################################################13.个股涨跌停列表####################################################
class LimitList(models.Model):
    """涨跌停列表"""
    trade_date = models.CharField(max_length=8, verbose_name='交易日期')
    ts_code = models.CharField(max_length=10, verbose_name='TS代码')
    industry = models.CharField(max_length=10, verbose_name='行业')
    name = models.CharField(max_length=50, verbose_name='股票名称')
    close = models.FloatField(null=True, blank=True, verbose_name='收盘价')
    pct_chg = models.FloatField(null=True, blank=True, verbose_name='涨跌幅')
    amount = models.FloatField(null=True, blank=True, verbose_name='成交额')
    limit_amount = models.FloatField(null=True, blank=True, verbose_name='跌停成交金额')#板上成交金额(成交价格为该股票跌停价的所有成交额的总和，涨停无此数据)
    float_mv = models.FloatField(null=True, blank=True, verbose_name='流通市值') #流通市值
    turnover_ratio = models.FloatField(null=True, blank=True, verbose_name='换手率')
    fd_amount = models.FloatField(null=True, blank=True, verbose_name='封单金额')
    first_time = models.CharField(max_length=8, null=True, blank=True, verbose_name='首次涨停时间')
    last_time = models.CharField(max_length=8, null=True, blank=True, verbose_name='最后封板时间')
    open_times = models.IntegerField(null=True, blank=True, verbose_name='打开次数')
    up_stat = models.CharField(max_length=8, null=True, blank=True, verbose_name='涨停统计（N/T T天有N次涨停）')
    limit_times = models.IntegerField(null=True, blank=True, verbose_name='连板数')
    limit = models.CharField(max_length=1, null=True, blank=True, verbose_name='涨跌停类型', help_text='U-涨停 D-跌停 Z-炸板')

    class Meta:
        verbose_name = '涨跌停列表'
        verbose_name_plural = verbose_name
        db_table = 'stock_limit_list'
        ordering = ['-trade_date']
        unique_together = ['ts_code', 'trade_date']
        indexes = [
            models.Index(fields=['trade_date'], name='limit_list_date_idx'),
        ]

    def __str__(self):
        return f"{self.ts_code} - {self.trade_date} - {'涨停' if self.limit == 'U' else '跌停'}"
#################################################14.指数分时数据####################################################
class IndexMinuteData(models.Model):
    """指数分时数据(1分钟)"""
    ts_code = models.CharField(max_length=20, verbose_name='TS代码')
    trade_time = models.DateTimeField(verbose_name='交易时间')
    open = models.FloatField(verbose_name='开盘点位')
    high = models.FloatField(verbose_name='最高点位')
    low = models.FloatField(verbose_name='最低点位')
    close = models.FloatField(verbose_name='收盘点位')
    vol = models.FloatField(verbose_name='成交量(手)')
    amount = models.FloatField(verbose_name='成交额(千元)')
    
    class Meta:
        verbose_name = '指数分时数据'
        verbose_name_plural = verbose_name
        db_table = 'index_minute_data'
        ordering = ['-trade_time']
        unique_together = ['ts_code', 'trade_time']
        indexes = [
            models.Index(fields=['trade_time'], name='index_minute_time_idx'),
        ]

    def __str__(self):
        return f"{self.ts_code} - {self.trade_time}"
    
#################################################15.IPO日历数据####################################################
#2025年8月25日后增加的数据表    
class IPO_NewShare(models.Model):
    """新股发行数据"""
    ts_code = models.CharField(max_length=10, verbose_name='TS代码')
    sub_code = models.CharField(max_length=10, verbose_name='申购代码')
    name = models.CharField(max_length=100, verbose_name='股票名称')
    ipo_date = models.CharField(max_length=8, verbose_name='上网发行日期')
    issue_date = models.CharField(null=True, blank=True,max_length=8, verbose_name='上市日期')
    amount = models.FloatField(null=True, blank=True, verbose_name='发行总量(万股)')
    market_amount = models.FloatField(null=True, blank=True, verbose_name='上网发行总量(万股)')
    price = models.FloatField(null=True, blank=True, verbose_name='发行价格')
    pe = models.FloatField(null=True, blank=True, verbose_name='市盈率')
    limit_amount = models.FloatField(null=True, blank=True, verbose_name='申购上限(万股)')
    funds = models.FloatField(null=True, blank=True, verbose_name='募集资金(亿元)')
    ballot = models.FloatField(null=True, blank=True, verbose_name='中签率')

    class Meta:
        verbose_name = '新股发行数据'
        verbose_name_plural = verbose_name
        db_table = 'stock_ipo_new'
        ordering = ['-ipo_date']

    def __str__(self):
        return f"{self.ts_code} - {self.name}"

##################################################16.股票ST数据####################################################
class Stock_ST(models.Model):
    """ST股票列表"""
    ts_code = models.CharField(max_length=10, verbose_name='TS代码')
    name = models.CharField(max_length=20, verbose_name='股票名称')
    trade_date = models.CharField(max_length=8, verbose_name='交易日期')
    type = models.CharField(max_length=10, verbose_name='ST类型')
    type_name = models.CharField(max_length=20, verbose_name='ST类型名称')

    class Meta:
        verbose_name = 'ST股票列表'
        verbose_name_plural = verbose_name
        db_table = 'stock_set_st'
        ordering = ['-trade_date']

    def __str__(self):
        return f"{self.ts_code} - {self.name}"

#################################################17.股票公司数据####################################################
class StockCompany(models.Model):
    """公司信息"""
    ts_code = models.CharField(max_length=10, verbose_name='TS代码')
    com_name = models.CharField(max_length=100, null=True,verbose_name='公司名称')
    manager = models.CharField(max_length=20, null=True, blank=True, verbose_name='公司管理层')
    main_business = models.TextField(null=True, blank=True, verbose_name='主营业务')
    business_scope = models.TextField(null=True, blank=True, verbose_name='经营范围')
    update_date = models.CharField(max_length=8, null=True, blank=True, verbose_name='更新日期')

    class Meta:
        verbose_name = '公司信息'
        verbose_name_plural = verbose_name
        db_table = 'stock_set_company'
        ordering = ['ts_code']

    def __str__(self):
        return f"{self.ts_code} - {self.com_name}"

#################################################18.股票周K线数据####################################################
class StockKDataWeekly(models.Model):
    """周线行情数据"""
    ts_code = models.CharField(max_length=10, verbose_name='TS代码')
    trade_date = models.CharField(max_length=8, verbose_name='交易日期')
    close = models.FloatField(null=True, blank=True, verbose_name='收盘价')
    open = models.FloatField(null=True, blank=True, verbose_name='开盘价')
    high = models.FloatField(null=True, blank=True, verbose_name='最高价')
    low = models.FloatField(null=True, blank=True, verbose_name='最低价')
    pre_close = models.FloatField(null=True, blank=True, verbose_name='昨收价')
    change = models.FloatField(null=True, blank=True, verbose_name='涨跌额')
    pct_chg = models.FloatField(null=True, blank=True, verbose_name='涨跌幅')
    vol = models.FloatField(null=True, blank=True, verbose_name='成交量')
    amount = models.FloatField(null=True, blank=True, verbose_name='成交额')

    class Meta:
        verbose_name = '周线行情数据'
        verbose_name_plural = verbose_name
        db_table = 'stock_kdata_weekly'
        ordering = ['-trade_date']
        unique_together = ['ts_code', 'trade_date']

    def __str__(self):
        return f"{self.ts_code} - {self.trade_date}"


#################################################19.股票月K线数据####################################################
class StockKDataMonthly(models.Model):
    """月线行情数据"""
    ts_code = models.CharField(max_length=10, verbose_name='TS代码')
    trade_date = models.CharField(max_length=8, verbose_name='交易日期')
    close = models.FloatField(null=True, blank=True, verbose_name='收盘价')
    open = models.FloatField(null=True, blank=True, verbose_name='开盘价')
    high = models.FloatField(null=True, blank=True, verbose_name='最高价')
    low = models.FloatField(null=True, blank=True, verbose_name='最低价')
    pre_close = models.FloatField(null=True, blank=True, verbose_name='昨收价')
    change = models.FloatField(null=True, blank=True, verbose_name='涨跌额')
    pct_chg = models.FloatField(null=True, blank=True, verbose_name='涨跌幅')
    vol = models.FloatField(null=True, blank=True, verbose_name='成交量')
    amount = models.FloatField(null=True, blank=True, verbose_name='成交额')

    class Meta:
        verbose_name = '月线行情数据'
        verbose_name_plural = verbose_name
        db_table = 'stock_kdata_monthly'
        ordering = ['-trade_date']
        unique_together = ['ts_code', 'trade_date']

    def __str__(self):
        return f"{self.ts_code} - {self.trade_date}"
#################################################20.沪深股通交易前20数据####################################################
class StockHSGT20(models.Model):
    """沪深股通交易前20数据"""
    trade_date = models.CharField(max_length=8, verbose_name='交易日期')
    ts_code = models.CharField(max_length=10, verbose_name='TS代码')
    name = models.CharField(max_length=100, verbose_name='股票名称')
    close = models.FloatField(null=True, blank=True, verbose_name='收盘价')
    change = models.FloatField(null=True, blank=True, verbose_name='涨跌额')
    rank = models.IntegerField(verbose_name='排名')
    market_type = models.CharField(max_length=10, verbose_name='市场类型')
    amount = models.FloatField(null=True, blank=True, verbose_name='成交金额')
    net_amount = models.FloatField(null=True, blank=True, verbose_name='净成交金额')
    buy = models.FloatField(null=True, blank=True, verbose_name='买入金额')
    sell = models.FloatField(null=True, blank=True, verbose_name='卖出金额')

    class Meta:
        verbose_name = '沪深股通交易前20数据'
        verbose_name_plural = verbose_name
        db_table = 'stock_hsgt_top20'
        ordering = ['-trade_date']
        unique_together = ['ts_code', 'trade_date']

    def __str__(self):
        return f"{self.ts_code} - {self.trade_date}"

#################################################21.股票筹码分布数据####################################################
class StockChipDistribution(models.Model):
    """筹码分布数据"""
    ts_code = models.CharField(max_length=10, verbose_name='TS代码')
    trade_date = models.CharField(max_length=8, verbose_name='交易日期')
    his_low = models.FloatField(null=True, blank=True, verbose_name='历史最低价')
    his_high = models.FloatField(null=True, blank=True, verbose_name='历史最高价')
    cost_5pct = models.FloatField(null=True, blank=True, verbose_name='5%成本价')
    cost_15pct = models.FloatField(null=True, blank=True, verbose_name='15%成本价')
    cost_50pct = models.FloatField(null=True, blank=True, verbose_name='50%成本价')
    cost_85pct = models.FloatField(null=True, blank=True, verbose_name='85%成本价')
    cost_95pct = models.FloatField(null=True, blank=True, verbose_name='95%成本价')
    weight_avg = models.FloatField(null=True, blank=True, verbose_name='加权平均成本')
    winner_rate = models.FloatField(null=True, blank=True, verbose_name='获利比例')

    class Meta:
        verbose_name = '筹码分布数据'
        verbose_name_plural = verbose_name
        db_table = 'stock_chip_dist'
        ordering = ['-trade_date']
        unique_together = ['ts_code', 'trade_date']

    def __str__(self):
        return f"{self.ts_code} - {self.trade_date}"

#################################################22.市场融资融券数据####################################################
class MarketRzrq(models.Model):
    """市场融资融券数据"""
    trade_date = models.CharField(max_length=8, verbose_name='交易日期')
    exchange_id = models.CharField(max_length=10, verbose_name='交易所代码')
    rzye = models.FloatField(null=True, blank=True, verbose_name='融资余额(元)')
    rzmre = models.FloatField(null=True, blank=True, verbose_name='融资买入额(元)')
    rzche = models.FloatField(null=True, blank=True, verbose_name='融资偿还额(元)')
    rqye = models.FloatField(null=True, blank=True, verbose_name='融券余额(元)')
    rqmcl = models.FloatField(null=True, blank=True, verbose_name='融券卖出量(股)')
    rzrqye = models.FloatField(null=True, blank=True, verbose_name='融资融券余额(元)')
    rqyl = models.FloatField(null=True, blank=True, verbose_name='融券余量(股)')

    class Meta:
        verbose_name = '市场融资融券数据'
        verbose_name_plural = verbose_name
        db_table = 'market_rzrq'
        ordering = ['-trade_date']
        unique_together = ['trade_date', 'exchange_id']

    def __str__(self):
        return f"{self.exchange_id} - {self.trade_date}"

#################################################23.个股融资融券数据####################################################
class Stock_Rzrq(models.Model):
    """个股融资融券数据"""
    trade_date = models.CharField(max_length=8, verbose_name='交易日期')
    ts_code = models.CharField(max_length=10, verbose_name='TS代码')
    rzye = models.FloatField(null=True, blank=True, verbose_name='融资余额(元)')
    rqye = models.FloatField(null=True, blank=True, verbose_name='融券余额(元)')
    rzmre = models.FloatField(null=True, blank=True, verbose_name='融资买入额(元)')
    rqyl = models.FloatField(null=True, blank=True, verbose_name='融券余量(股)')
    rzche = models.FloatField(null=True, blank=True, verbose_name='融资偿还额(元)')
    rqchl = models.FloatField(null=True, blank=True, verbose_name='融券偿还量(股)')
    rqmcl = models.FloatField(null=True, blank=True, verbose_name='融券卖出量(股)')
    rzrqye = models.FloatField(null=True, blank=True, verbose_name='融资融券余额(元)')

    class Meta:
        verbose_name = '个股融资融券数据'
        verbose_name_plural = verbose_name
        db_table = 'stock_rzrq'
        ordering = ['-trade_date']
        unique_together = ['trade_date', 'ts_code']

    def __str__(self):
        return f"{self.ts_code} - {self.trade_date}"

#################################################24.个股资金流向数据####################################################
class StockMoneyFlow(models.Model):
    """
    个股资金流向数据
    """
    ts_code = models.CharField(max_length=10, verbose_name='TS代码')
    trade_date = models.CharField(max_length=8, verbose_name='交易日期')
    buy_sm_vol = models.FloatField(null=True, blank=True, verbose_name='小单买入量(手)')
    buy_sm_amount = models.FloatField(null=True, blank=True, verbose_name='小单买入金额(万元)')
    sell_sm_vol = models.FloatField(null=True, blank=True, verbose_name='小单卖出量(手)')
    sell_sm_amount = models.FloatField(null=True, blank=True, verbose_name='小单卖出金额(万元)')
    buy_md_vol = models.FloatField(null=True, blank=True, verbose_name='中单买入量(手)')
    buy_md_amount = models.FloatField(null=True, blank=True, verbose_name='中单买入金额(万元)')
    sell_md_vol = models.FloatField(null=True, blank=True, verbose_name='中单卖出量(手)')
    sell_md_amount = models.FloatField(null=True, blank=True, verbose_name='中单卖出金额(万元)')
    buy_lg_vol = models.FloatField(null=True, blank=True, verbose_name='大单买入量(手)')
    buy_lg_amount = models.FloatField(null=True, blank=True, verbose_name='大单买入金额(万元)')
    sell_lg_vol = models.FloatField(null=True, blank=True, verbose_name='大单卖出量(手)')
    sell_lg_amount = models.FloatField(null=True, blank=True, verbose_name='大单卖出金额(万元)')
    buy_elg_vol = models.FloatField(null=True, blank=True, verbose_name='特大单买入量(手)')
    buy_elg_amount = models.FloatField(null=True, blank=True, verbose_name='特大单买入金额(万元)')
    sell_elg_vol = models.FloatField(null=True, blank=True, verbose_name='特大单卖出量(手)')
    sell_elg_amount = models.FloatField(null=True, blank=True, verbose_name='特大单卖出金额(万元)')
    net_mf_vol = models.FloatField(null=True, blank=True, verbose_name='净流入量(手)')
    net_mf_amount = models.FloatField(null=True, blank=True, verbose_name='净流入金额(万元)')

    class Meta:
        verbose_name = '个股资金流向数据'
        verbose_name_plural = verbose_name
        db_table = 'stock_moneyflow'
        ordering = ['-trade_date']
        unique_together = ['ts_code', 'trade_date']

    def __str__(self):
        return f"{self.ts_code} - {self.trade_date}"
#################################################25.同花顺个股资金流向数据####################################################
class StockMoneyFlowTHS(models.Model):
    """同花顺个股资金流向数据"""
    trade_date = models.CharField(max_length=8, verbose_name='交易日期')
    ts_code = models.CharField(max_length=10, verbose_name='TS代码')
    name = models.CharField(max_length=100, verbose_name='股票名称')
    pct_change = models.FloatField(null=True, blank=True, verbose_name='涨跌幅')
    latest = models.FloatField(null=True, blank=True, verbose_name='最新价')
    net_amount = models.FloatField(null=True, blank=True, verbose_name='净流入额')
    net_d5_amount = models.FloatField(null=True, blank=True, verbose_name='5日净流入额')
    buy_lg_amount = models.FloatField(null=True, blank=True, verbose_name='大单买入金额')
    buy_lg_amount_rate = models.FloatField(null=True, blank=True, verbose_name='大单买入占比')
    buy_md_amount = models.FloatField(null=True, blank=True, verbose_name='中单买入金额')
    buy_md_amount_rate = models.FloatField(null=True, blank=True, verbose_name='中单买入占比')
    buy_sm_amount = models.FloatField(null=True, blank=True, verbose_name='小单买入金额')
    buy_sm_amount_rate = models.FloatField(null=True, blank=True, verbose_name='小单买入占比')

    class Meta:
        verbose_name = '同花顺资金流向数据'
        verbose_name_plural = verbose_name
        db_table = 'stock_moneyflow_ths'
        ordering = ['-trade_date']
        unique_together = ['ts_code', 'trade_date']

    def __str__(self):
        return f"{self.ts_code} - {self.trade_date}"
#################################################26.同花顺概念板块资金流向数据####################################################
class BlockMoneyFlowTHS_GN(models.Model):
    """同花顺概念板块资金流向数据"""
    trade_date = models.CharField(max_length=8, verbose_name='交易日期')
    ts_code = models.CharField(max_length=20, verbose_name='板块代码')
    name = models.CharField(max_length=100, verbose_name='板块名称')
    lead_stock = models.CharField(max_length=10, null=True, blank=True, verbose_name='领涨股代码')
    close_price = models.FloatField(null=True, blank=True, verbose_name='收盘价')
    pct_change = models.FloatField(null=True, blank=True, verbose_name='涨跌幅')
    industry_index = models.FloatField(null=True, blank=True, verbose_name='行业指数')
    company_num = models.IntegerField(null=True, blank=True, verbose_name='成分股数量')
    pct_change_stock = models.FloatField(null=True, blank=True, verbose_name='成分股平均涨跌幅')
    net_buy_amount = models.FloatField(null=True, blank=True, verbose_name='净买入金额')
    net_sell_amount = models.FloatField(null=True, blank=True, verbose_name='净卖出金额')
    net_amount = models.FloatField(null=True, blank=True, verbose_name='净流入金额')

    class Meta:
        verbose_name = '同花顺概念板块资金流向数据'
        verbose_name_plural = verbose_name
        db_table = 'block_moneyflow_ths_gn'
        ordering = ['-trade_date']
        unique_together = ['ts_code', 'trade_date']

    def __str__(self):
        return f"{self.ts_code} - {self.trade_date}"    
#################################################27.同花顺行业板块资金流向数据####################################################
class BlockMoneyFlowTHS_HY(models.Model):
    """同花顺行业板块资金流向数据"""
    trade_date = models.CharField(max_length=8, verbose_name='交易日期')
    ts_code = models.CharField(max_length=20, verbose_name='板块代码')
    industry = models.CharField(max_length=100, verbose_name='行业名称')
    lead_stock = models.CharField(max_length=10, null=True, blank=True, verbose_name='领涨股代码')
    close = models.FloatField(null=True, blank=True, verbose_name='收盘价')
    pct_change = models.FloatField(null=True, blank=True, verbose_name='涨跌幅')
    company_num = models.IntegerField(null=True, blank=True, verbose_name='成分股数量')
    pct_change_stock = models.FloatField(null=True, blank=True, verbose_name='成分股平均涨跌幅')
    close_price = models.FloatField(null=True, blank=True, verbose_name='收盘点位')
    net_buy_amount = models.FloatField(null=True, blank=True, verbose_name='净买入金额')
    net_sell_amount = models.FloatField(null=True, blank=True, verbose_name='净卖出金额')
    net_amount = models.FloatField(null=True, blank=True, verbose_name='净流入金额')

    class Meta:
        verbose_name = '同花顺行业板块资金流向数据'
        verbose_name_plural = verbose_name
        db_table = 'block_moneyflow_ths_hy'
        ordering = ['-trade_date']
        unique_together = ['ts_code', 'trade_date']

    def __str__(self):
        return f"{self.ts_code} - {self.trade_date}"
    
#################################################28.大盘资金流向数据（东方财富）####################################################
class MarketMoneyFlowDC(models.Model):
    """市场资金流向数据"""
    trade_date = models.CharField(max_length=8, verbose_name='交易日期')
    close_sh = models.FloatField(null=True, blank=True, verbose_name='上证收盘价')
    pct_change_sh = models.FloatField(null=True, blank=True, verbose_name='上证涨跌幅')
    close_sz = models.FloatField(null=True, blank=True, verbose_name='深证收盘价')
    pct_change_sz = models.FloatField(null=True, blank=True, verbose_name='深证涨跌幅')
    net_amount = models.FloatField(null=True, blank=True, verbose_name='净流入金额')
    net_amount_rate = models.FloatField(null=True, blank=True, verbose_name='净流入占比')
    buy_elg_amount = models.FloatField(null=True, blank=True, verbose_name='超大单买入金额')
    buy_elg_amount_rate = models.FloatField(null=True, blank=True, verbose_name='超大单买入占比')
    buy_lg_amount = models.FloatField(null=True, blank=True, verbose_name='大单买入金额')
    buy_lg_amount_rate = models.FloatField(null=True, blank=True, verbose_name='大单买入占比')
    buy_md_amount = models.FloatField(null=True, blank=True, verbose_name='中单买入金额')
    buy_md_amount_rate = models.FloatField(null=True, blank=True, verbose_name='中单买入占比')
    buy_sm_amount = models.FloatField(null=True, blank=True, verbose_name='小单买入金额')
    buy_sm_amount_rate = models.FloatField(null=True, blank=True, verbose_name='小单买入占比')

    class Meta:
        verbose_name = '市场大盘资金流向数据'
        verbose_name_plural = verbose_name
        db_table = 'market_moneyflow_dc'
        ordering = ['-trade_date']
        unique_together = ['trade_date']

    def __str__(self):
        return f"{self.trade_date}"

#################################################29.板块KPL数据####################################################
class BlockConceptKPL(models.Model):
    """板块KPL数据"""
    trade_date = models.CharField(max_length=8, verbose_name='交易日期')
    ts_code = models.CharField(max_length=20, verbose_name='板块代码')
    name = models.CharField(max_length=100, verbose_name='板块名称')
    z_t_num = models.IntegerField(verbose_name='涨停家数')
    up_num = models.IntegerField(verbose_name='排名上升数')

    class Meta:
        verbose_name = '板块KPL数据'
        verbose_name_plural = verbose_name
        db_table = 'block_concept_kpl'
        ordering = ['-trade_date']
        unique_together = ['trade_date', 'ts_code']

    def __str__(self):
        return f"{self.ts_code} - {self.trade_date}"

#################################################30.开盘啦题材成分股####################################################
class BlockConceptStockKpl(models.Model):
    """开盘啦题材成分股"""
    ts_code = models.CharField(max_length=20, verbose_name='题材代码')
    name = models.CharField(max_length=100, verbose_name='题材名称')
    con_name = models.CharField(max_length=100, verbose_name='成分股名称')
    con_code = models.CharField(max_length=20, verbose_name='成分股代码')
    trade_date = models.CharField(max_length=8, verbose_name='交易日期')
    desc = models.TextField(null=True, blank=True, verbose_name='题材描述')
    hot_num = models.IntegerField(null=True, blank=True, verbose_name='热度')

    class Meta:
        verbose_name = '开盘啦题材成分股'
        verbose_name_plural = verbose_name
        db_table = 'block_concept_stock_kpl'
        ordering = ['-trade_date']
        unique_together = ['ts_code', 'con_code', 'trade_date']

    def __str__(self):
        return f"{self.name} - {self.con_name}"
    
#################################################31.开盘啦榜单数据####################################################
class BlockConceptListKpl(models.Model):
    """开盘啦榜单数据"""
    ts_code = models.CharField(max_length=20, verbose_name='题材代码')
    name = models.CharField(max_length=100, verbose_name='个股名称')
    trade_date = models.CharField(max_length=8, verbose_name='交易日期')
    lu_time = models.CharField(max_length=20, null=True, blank=True, verbose_name='涨停时间')
    ld_time = models.CharField(max_length=20, null=True, blank=True, verbose_name='跌停时间')
    open_time = models.CharField(max_length=20, null=True, blank=True, verbose_name='开板时间')
    last_time = models.CharField(max_length=20, null=True, blank=True, verbose_name='最后涨停时间')
    lu_desc = models.TextField(null=True, blank=True, verbose_name='涨停原因')
    tag = models.CharField(max_length=100, null=True, blank=True, verbose_name='标签')
    theme = models.CharField(max_length=100, null=True, blank=True, verbose_name='主题')
    net_change = models.FloatField(null=True, blank=True, verbose_name='主力净额(元)')
    bid_amount = models.FloatField(null=True, blank=True, verbose_name='竞价成交额(元)')
    status = models.CharField(max_length=20, null=True, blank=True, verbose_name='状态（N连板）')
    bid_change = models.FloatField(null=True, blank=True, verbose_name='竞价净额')
    bid_turnover = models.FloatField(null=True, blank=True, verbose_name='竞价换手率')
    lu_bid_vol = models.FloatField(null=True, blank=True, verbose_name='涨停委买额')
    pct_chg = models.FloatField(null=True, blank=True, verbose_name='涨跌幅')
    bid_pct_chg = models.FloatField(null=True, blank=True, verbose_name='竞价涨跌幅')
    rt_pct_chg = models.FloatField(null=True, blank=True, verbose_name='实时涨跌幅')
    limit_order = models.FloatField(null=True, blank=True, verbose_name='涨停封单')
    amount = models.FloatField(null=True, blank=True, verbose_name='成交金额')
    turnover_rate = models.FloatField(null=True, blank=True, verbose_name='换手率')
    free_float = models.FloatField(null=True, blank=True, verbose_name='自由流通量')
    lu_limit_order = models.FloatField(null=True, blank=True, verbose_name='最大涨停封单')

    class Meta:
        verbose_name = '开盘啦榜单数据'
        verbose_name_plural = verbose_name
        db_table = 'block_concept_list_kpl'
        ordering = ['-trade_date']
        unique_together = ['ts_code', 'trade_date']

    def __str__(self):
        return f"{self.name} - {self.trade_date}"
#################################################32.龙虎榜数据####################################################
class MarketTopList(models.Model):
    """
    龙虎榜每日明细数据模型
    """
    trade_date = models.CharField(max_length=8, verbose_name='交易日期')
    ts_code = models.CharField(max_length=20, verbose_name='股票代码')
    name = models.CharField(max_length=50, verbose_name='股票名称')
    close = models.FloatField(verbose_name='收盘价')
    pct_change = models.FloatField(verbose_name='涨跌幅')
    turnover_rate = models.FloatField(verbose_name='换手率')
    amount = models.FloatField(verbose_name='成交额(万元)')
    l_sell = models.FloatField(verbose_name='龙虎榜卖出额(万元)')
    l_buy = models.FloatField(verbose_name='龙虎榜买入额(万元)')
    l_amount = models.FloatField(verbose_name='龙虎榜成交额(万元)')
    net_amount = models.FloatField(verbose_name='净买入额(万元)')
    net_rate = models.FloatField(verbose_name='净买额占比')
    amount_rate = models.FloatField(verbose_name='成交额占比')
    float_values = models.FloatField(verbose_name='流通市值(亿元)')
    reason = models.CharField(max_length=200, verbose_name='上榜原因')

    class Meta:
        db_table = 'market_lhb_top_list'
        verbose_name = '龙虎榜每日明细'
        verbose_name_plural = verbose_name
        unique_together = ('trade_date', 'ts_code')

    def __str__(self):
        return f"{self.trade_date} {self.ts_code} {self.name}"
#################################################33.龙虎榜机构成交明细####################################################
class MarketTopDetail(models.Model):
    """
    龙虎榜机构成交明细数据模型
    """
    trade_date = models.CharField(max_length=8, verbose_name='交易日期')
    ts_code = models.CharField(max_length=20, verbose_name='股票代码')
    exalter = models.CharField(max_length=100, verbose_name='营业部名称')
    buy = models.FloatField(verbose_name='买入额(万元)')
    buy_rate = models.FloatField(verbose_name='买入占比')
    sell = models.FloatField(verbose_name='卖出额(万元)')
    sell_rate = models.FloatField(verbose_name='卖出占比')
    net_buy = models.FloatField(verbose_name='净买入额(万元)')
    side = models.CharField(max_length=10, verbose_name='买卖方向')
    reason = models.CharField(max_length=200, verbose_name='上榜原因')

    class Meta:
        db_table = 'market_lhb_top_detail'
        verbose_name = '龙虎榜机构成交明细'
        verbose_name_plural = verbose_name
        # unique_together = ('trade_date', 'ts_code', 'exalter', 'side')

    def __str__(self):
        return f"{self.trade_date} {self.ts_code} {self.exalter}"
    
#################################################34.游资名录####################################################
class MarketHMInfo(models.Model):
    """游资名录"""
    name = models.CharField(max_length=100, verbose_name='游资名称')
    desc = models.TextField(null=True, blank=True, verbose_name='游资描述')
    orgs = models.CharField(max_length=100, null=True, blank=True, verbose_name='关联机构')

    class Meta:
        verbose_name = '游资名录'
        verbose_name_plural = verbose_name
        db_table = 'market_hm_info'

    def __str__(self):
        return self.name
    

#################################################35.游资每日交易明细####################################################
class MarketHMDetailDaily(models.Model):
    """游资每日交易明细"""
    trade_date = models.CharField(max_length=8, verbose_name='交易日期')
    ts_code = models.CharField(max_length=10, verbose_name='股票代码')
    ts_name = models.CharField(max_length=50, verbose_name='股票名称')
    buy_amount = models.FloatField(null=True, blank=True, verbose_name='买入金额')
    sell_amount = models.FloatField(null=True, blank=True, verbose_name='卖出金额')
    net_amount = models.FloatField(null=True, blank=True, verbose_name='净买入金额')
    hm_name = models.CharField(max_length=100, verbose_name='游资名称')
    hm_orgs = models.CharField(max_length=100, null=True, blank=True, verbose_name='关联机构')

    class Meta:
        verbose_name = '游资每日交易明细'
        verbose_name_plural = verbose_name
        db_table = 'market_hm_detail_daily'
        # unique_together = ['trade_date', 'ts_code', 'hm_name']

    def __str__(self):
        return f"{self.trade_date} - {self.ts_code} - {self.hm_name}"
    
##################################################36.同花顺热门榜单数据####################################################
class MarketHotListTHS(models.Model):
    """同花顺热门榜单数据"""
    trade_date = models.CharField(max_length=8, verbose_name='交易日期')
    data_type = models.CharField(max_length=20, verbose_name='数据类型')
    ts_code = models.CharField(max_length=10, verbose_name='TS代码')
    ts_name = models.CharField(max_length=100, verbose_name='股票名称')
    rank = models.IntegerField(verbose_name='排名', null=True, blank=True)
    pct_change = models.FloatField(verbose_name='涨跌幅', null=True, blank=True)
    current_price = models.FloatField(verbose_name='当前价格', null=True, blank=True)
    hot = models.CharField(max_length=10, verbose_name='热度', null=True, blank=True)
    concept = models.CharField(max_length=100, verbose_name='概念', null=True, blank=True)
    rank_time = models.CharField(max_length=20, verbose_name='排名时间', null=True, blank=True)
    rank_reason = models.TextField(verbose_name='排名原因', null=True, blank=True)

    class Meta:
        verbose_name = '同花顺热门榜单数据'
        verbose_name_plural = verbose_name
        db_table = 'market_hot_list_ths'
        ordering = ['-trade_date']

    def __str__(self):
        return f"{self.trade_date} - {self.ts_name}"
    

    ##################################################37.东方财富热门榜单数据####################################################
class MarketHotListDc(models.Model):
    """东方财富热门榜单数据"""
    trade_date = models.CharField(max_length=8, verbose_name='交易日期')
    data_type = models.CharField(max_length=20, verbose_name='数据类型')
    ts_code = models.CharField(max_length=10, verbose_name='TS代码')
    ts_name = models.CharField(max_length=100, verbose_name='股票名称')
    rank = models.IntegerField(verbose_name='排名', null=True, blank=True)
    pct_change = models.FloatField(verbose_name='涨跌幅', null=True, blank=True)
    current_price = models.FloatField(verbose_name='当前价格', null=True, blank=True)
    hot = models.CharField(max_length=10, verbose_name='热度', null=True, blank=True)
    concept = models.CharField(max_length=100, verbose_name='概念', null=True, blank=True)
    rank_time = models.CharField(max_length=20, verbose_name='排名时间', null=True, blank=True)

    class Meta:
        verbose_name = '东方财富热门榜单数据'
        verbose_name_plural = verbose_name
        db_table = 'market_hot_list_dc'
        ordering = ['-trade_date']

##################################################38.个股十大股东数据####################################################
class StockHolderTop10(models.Model):
    """个股十大股东数据"""
    ts_code = models.CharField(max_length=10, verbose_name='TS代码')
    ann_date = models.CharField(max_length=8, verbose_name='公告日期')
    end_date = models.CharField(max_length=8, verbose_name='截止日期')
    holder_name = models.CharField(max_length=100, verbose_name='股东名称')
    hold_amount = models.FloatField(null=True, blank=True, verbose_name='持股数量')
    hold_ratio = models.FloatField(null=True, blank=True, verbose_name='持股比例')
    hold_float_ratio = models.FloatField(null=True, blank=True, verbose_name='流通股持股比例')
    hold_change = models.FloatField(null=True, blank=True, verbose_name='持股变动')
    holder_type = models.CharField(max_length=20, null=True, blank=True, verbose_name='股东类型')

    class Meta:
        verbose_name = '个股十大股东'
        verbose_name_plural = verbose_name
        db_table = 'stock_holder_top10'
        ordering = ['-end_date']
        unique_together = ['ts_code', 'ann_date', 'end_date', 'holder_name', 'hold_amount']

    def __str__(self):
        return f"{self.ts_code} - {self.holder_name} - {self.end_date}"

##################################################39.个股十大流通股东数据####################################################
class StockHolderTop10Float(models.Model):
    """个股十大流通股东数据"""
    ts_code = models.CharField(max_length=10, verbose_name='TS代码')
    ann_date = models.CharField(max_length=8, verbose_name='公告日期')
    end_date = models.CharField(max_length=8, verbose_name='截止日期')
    holder_name = models.CharField(max_length=100, verbose_name='股东名称')
    hold_amount = models.FloatField(null=True, blank=True, verbose_name='持股数量')
    hold_ratio = models.FloatField(null=True, blank=True, verbose_name='持股比例')
    hold_float_ratio = models.FloatField(null=True, blank=True, verbose_name='流通股持股比例')
    hold_change = models.FloatField(null=True, blank=True, verbose_name='持股变动')
    holder_type = models.CharField(max_length=20, null=True, blank=True, verbose_name='股东类型')             
    
    class Meta:
        verbose_name = '个股十大流通股东'
        verbose_name_plural = verbose_name
        db_table = 'stock_holder_top10_float'
        ordering = ['-end_date']
        unique_together = ['ts_code', 'ann_date', 'end_date', 'holder_name','hold_amount']            
        
    def __str__(self):    
        return f"{self.ts_code} - {self.holder_name} - {self.end_date}"
    

    ##################################################40.大宗交易数据####################################################
class MarketBigTrade(models.Model):
    """大宗交易数据"""
    ts_code = models.CharField(max_length=10, verbose_name='TS代码')
    trade_date = models.CharField(max_length=8, verbose_name='交易日期')
    price = models.FloatField(verbose_name='成交价格')
    vol = models.FloatField(verbose_name='成交数量')
    amount = models.FloatField(verbose_name='成交金额')
    buyer = models.CharField(max_length=100, verbose_name='买方营业部',null=True, blank=True)
    seller = models.CharField(max_length=100, verbose_name='卖方营业部',null=True, blank=True)

    class Meta:
        verbose_name = '大宗交易数据'
        verbose_name_plural = verbose_name
        db_table = 'market_big_trade'
        ordering = ['-trade_date']

    def __str__(self):
        return f"{self.ts_code} - {self.trade_date}"

##################################################41.场内基金列表####################################################
class MarketFundEList(models.Model):
    """场内基金列表"""
    ts_code = models.CharField(max_length=20, primary_key=True, verbose_name='基金代码')
    name = models.CharField(max_length=100, verbose_name='简称')
    management = models.CharField(max_length=100, null=True, blank=True, verbose_name='管理人')
    custodian = models.CharField(max_length=100, null=True, blank=True, verbose_name='托管人')
    fund_type = models.CharField(max_length=50, null=True, blank=True, verbose_name='投资类型')
    list_date = models.CharField(max_length=8, null=True, blank=True, verbose_name='上市日期')
    issue_amount = models.FloatField(null=True, blank=True, verbose_name='发行份额(亿)')
    status = models.CharField(max_length=20, null=True, blank=True, verbose_name='状态')
    invest_type = models.CharField(max_length=50, null=True, blank=True, verbose_name='投资风格')

    class Meta:
        verbose_name = '场内基金列表'
        verbose_name_plural = verbose_name
        db_table = 'market_fund_e_list'
        ordering = ['ts_code']

    def __str__(self):
        return f"{self.ts_code} - {self.name}"


##################################################42.场内基金详细数据####################################################
class MarketFundEDetail(models.Model):
    """场内基金详细数据"""
    ts_code = models.CharField(max_length=20, verbose_name='基金代码')
    ann_date = models.CharField(max_length=8, verbose_name='公告日期')
    end_date = models.CharField(max_length=8, verbose_name='截止日期')
    symbol = models.CharField(max_length=10, verbose_name='股票代码')
    mkv = models.FloatField(null=True, blank=True, verbose_name='市值(万元)')
    amount = models.FloatField(null=True, blank=True, verbose_name='金额(万元)')
    stk_mkv_ratio = models.FloatField(null=True, blank=True, verbose_name='股票市值占比(%)')
    stk_float_ratio = models.FloatField(null=True, blank=True, verbose_name='股票流通市值占比(%)')

    class Meta:
        verbose_name = '场内基金详细数据'
        verbose_name_plural = verbose_name
        db_table = 'market_fund_e_detail'
        ordering = ['-end_date']
        unique_together = ['ts_code', 'ann_date', 'end_date', 'symbol']

    def __str__(self):
        return f"{self.ts_code} - {self.end_date} - {self.symbol}"